package com.leo.train.member.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leo.train.common.context.LoginMemberContext;
import com.leo.train.common.exception.BusinessException;
import com.leo.train.common.exception.BusinessExceptionEnum;
import com.leo.train.common.resp.PageResult;
import com.leo.train.common.util.SnowUtil;
import com.leo.train.member.domain.Passenger;
import com.leo.train.member.dto.PassengerQueryDto;
import com.leo.train.member.mapper.PassengerMapper;
import com.leo.train.member.service.PassengerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PassengerServiceImpl implements PassengerService {
    @Autowired
    private PassengerMapper passengerMapper;
    /**
     * 保存旅客
     * @param passenger
     */
    @Override
    public void save(Passenger passenger) {
        // 获取线程中存储的会员id
        Long memberId = LoginMemberContext.getId();
        passenger.setMemberId(memberId);
        // 判断当前乘客的身份账号是否和列表所有乘客的身份证号一致
        List<Passenger> list = passengerMapper.selectListByMemberId(memberId);
        for(Passenger p :list) {
            if(p.getIdCard().equals(passenger.getIdCard())){
                throw new BusinessException(BusinessExceptionEnum.PASSENGER_ID_CARD_UNIQUE_ERROR);
            }
        }
        if(passenger.getId() == null){
            // 新增
            // 生成雪花id
            passenger.setId(SnowUtil.getSnowflakeNextId());
            passenger.setCreateTime(new Date());
            passenger.setUpdateTime(new Date());
            passengerMapper.insert(passenger);
        }else{
            // 更新
            Passenger passengerInDB = passengerMapper.selectByPrimaryKey(passenger.getId());
            // 判断当前乘客是否存在
            if(passengerInDB == null){
                throw new BusinessException(BusinessExceptionEnum.PASSENGER_NOT_EXIST);
            }
            passenger.setUpdateTime(new Date());
            passengerMapper.updateByPrimaryKeySelective( passenger);
        }
    }
    /**
     * 列表查询
     */
    @Override
    public PageResult list(PassengerQueryDto queryDto) {
        PageHelper.startPage(queryDto.getPage(), queryDto.getPageSize());
        Page<Passenger> page = passengerMapper.pageQuery(queryDto);
        long total = page.getTotal();
        List<Passenger> list = page.getResult();
        return new PageResult(total, list);
    }

    @Override
    public Passenger detail(Long id) {
        Passenger passenger = passengerMapper.selectByPrimaryKey(id);
        if(passenger == null){
            throw new BusinessException(BusinessExceptionEnum.PASSENGER_NOT_EXIST);
        }
        return passenger;
    }

    @Override
    public void delete(Long id) {
        // 判断当前乘客是否存在
        Passenger passenger = passengerMapper.selectByPrimaryKey(id);
        if(passenger == null){
            throw new BusinessException(BusinessExceptionEnum.PASSENGER_NOT_EXIST);
        }
        passengerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Passenger> queryMine() {
        // 获取线程中存储的会员id
        Long memberId = LoginMemberContext.getId();
        // 根据会员id查询所有乘客
        List<Passenger> list = passengerMapper.selectListByMemberId(memberId);
        return list;
    }

}
